package com.kajespero.model;

import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Text;
import com.kajespero.model.annotation.MappedValue;
import com.kajespero.model.annotation.enums.RelationType;
import com.kajespero.vo.ArticleVO;
import com.kajespero.webapp.editor.annotation.PropertyEditorSupport;
import com.kajespero.webapp.editor.impl.PagePropertyEditorSupport;
import com.kajespero.webapp.validator.annotation.Validator;
import com.kajespero.webapp.validator.impl.ArticleValidator;

@Validator(clazz=ArticleValidator.class)
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class ArticleBean extends IdentifierVOBean<Long> implements ArticleVO{

	/**
	 * 
	 */
	private static final long serialVersionUID = -3046414682133804302L;
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Key key;
	
	@Persistent
	private Long id;
	
	@Persistent
	private String title;
	
	@Persistent
	private Text value;
	
	@NotPersistent
	@PropertyEditorSupport(editor=PagePropertyEditorSupport.class, fieldName="page", attributeName="id")
	@MappedValue(type=PageBean.class, relationType=RelationType.MANY_TO_ONE, mappedBy="pageId")
	private PageBean page;
	
	@Persistent
	private Key pageId;
	
	@NotPersistent
	@MappedValue(type=UserBean.class, relationType=RelationType.MANY_TO_ONE, mappedBy="userId")
	private UserBean createBy;
	
	
	@Persistent
	private Key userId;
	
	@Persistent
	private Date createAt;
	
	@Persistent
	private Date modifyAt;
	
	@Persistent
	private Integer index;

	@Override
	public String getTitle() {
		return this.title;
	}

	@Override
	public Key getKey() {
		return this.key;
	}

	@Override
	public String getText() {
		if(this.value != null){
			return this.value.getValue();
		}
		return null;
	}

	@Override
	public PageBean getPage() {
		return this.page;
	}

	@Override
	public UserBean getCreateBy() {
		return this.createBy;
	}

	@Override
	public Date getCreateAt() {
		return this.createAt;
	}

	@Override
	public Date getModifyAt() {
		return this.modifyAt;
	}

	@Override
	public void setKey(Key key) {
		this.key = key;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setText(String text) {
		this.value = new Text(text);
	}

	public void setPage(PageBean page) {
		this.page = page;
	}

	public void setCreateBy(UserBean createBy) {
		this.createBy = createBy;
	}

	public void setCreateAt(Date createAt) {
		this.createAt = createAt;
	}

	public void setModifyAt(Date modifyAt) {
		this.modifyAt = modifyAt;
	}


	@Override
	public Long getId() {
		return this.id;
	}
	
	public void setId(Long id){
		this.id = id;
	}


	public Key getPageId() {
		return pageId;
	}


	public void setPageId(Key pageId) {
		this.pageId = pageId;
	}


	public Key getUserId() {
		return userId;
	}


	public void setUserId(Key userId) {
		this.userId = userId;
	}

	public Integer getIndex() {
		if(index == null){
			index = id.intValue();
		}
		return index;
	}

	public void setIndex(Integer index) {
		this.index = index;
	}

}
